package jdbc;

import jdbc.utill.DButil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 模拟登录
 */
public class LoginDemo {
    public static void main(String[] args) throws Exception {
        // 1 记录用户输入的内容
        System.out.println("请输入用户名");
        Scanner sc = new Scanner(System.in);
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        // 2 链接数据库 查询
       Connection connection = DButil.getConnection();
       Statement statement =connection.createStatement();
       String sql = "select id,username,password,nickname,age" +
               " from t_user where username = '" + username + "' and " +
               "password = '" + password + "'";
        System.out.println(sql);
        ResultSet resultSet = statement.executeQuery(sql);
        // 3 查询结果 判断
        if (resultSet.next()) {
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
        DButil.close(connection);
    }
}
